package cn.itsource.article.service.impl;

import cn.itsource.article.domain.Article;
import cn.itsource.article.mapper.ArticleMapper;

import cn.itsource.article.query.ArticleQuery;
import cn.itsource.article.service.IArticleService;

import cn.itsource.article.vo.ArticleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    @Transactional

    public void save(Article article) {
        if(article.getId() == null){
            articleMapper.insert(article);
        }else {
            articleMapper.updateByPrimaryKeySelective(article);
        }
    }

    @Override
    @Transactional

    public int deleteByPrimaryKey(Long id) {
        return articleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Article selectByPrimaryKey(Long id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Article> pageInfo(ArticleQuery articleQuery) {
        PageHelper.startPage(articleQuery.getCurrentPage(),articleQuery.getPageSize());
        List<Article> articles = articleMapper.selectAllByQuery(articleQuery);
        return new PageInfo<Article>(articles);
    }

    @Override
    @Transactional
    public void deleteByCollection(List<Article> articles) {
        articleMapper.deleteByConllection(articles);
    }

    @Override
    public List<ArticleVo> group() {
        return  articleMapper.group();
    }

    @Override
    public List<ArticleVo> createGroup() {
        return articleMapper.createGroup();
    }

    @Override
    public Article selectByarticleId(Long articleId) {
        return articleMapper.selectByPrimaryKey(articleId);
    }

    @Override
    public List<Article> aboutArticle(Long articleId) {
        return articleMapper.selectAboutArticleByArticleId(articleId);
    }

    @Override
    public void addNum(String articleId) {
        articleMapper.addNum(articleId);
    }

    @Override
    public void addWatch(String articleId) {
        articleMapper.addWarch(articleId);
    }

    @Override
    public List<Article> getList() {
       return articleMapper.getList();
    }


}


